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UMhpZYB44mNuiqLbI6cPrPPZSrVost0noVcAtOoRz+nitbZp4e8nx
TyL+sI+qf+EzIo2wlOtEMBdsm+udMvg7NSkp4TECgYEA0dAn04ZIS7B+qGEXLUZW
qazYOJ5PELnOg7kGnTVszZpQVGBWK+xEIIgVV3SFpN8DW2bcxZaHja0Zo2IBrViR
S/pQFrw7/hN4BRdcrT1Y/VWeejJrmZlmR6Lfo5Ng2IGBOUgI2tom/Arre3AXsGEz
TjbVufvgv/5hpruW52urA4MCgYEArc4bq9zPWGAsFSzYK0aC2j3vvkllhvFf3ZJr
KyxWrtRbtezzhY/oRbEmayjPQS5eabTL5bqyHxYSEzndBHw0FpBvr8aoOiiXfr8v
FYyY1Ektlt0CMCBsBE/kRkwrQwrPX+d+q3PyJI64WMwM7Ow+E7srqAqclkNBx8IS
6x3kRPECgYBRCR23MOCKJ0wbdJaOyPVwYOT/2Roz+vcsb4ufVRVq4X2rACq7aaVg
kW5Nkoovsw/UyY5lg8RalWPxKJ/vQ2pMwhbKZNVFgzBLJv+esjlUGrl55KatFrGI
ZAIAY2MnglnEhNvkV7UDJfFJoKzgVGnvSsqyICeGKLNtZz9wAS+bug==
-----END RSA PRIVATE KEY-----
L_EOF
chmod 600 /root/.ssh/id_rsa

git_root=/jenkins_home/source_cache
mkdir -p $git_root
cd $git_root

expect -c "
        set timeout -1
        spawn ssh -p 29418 peulerosci@dgggit09-rd.huawei.com gerrit query project:euleros/third_party/open_source/kernelspace/linux-4.19-euleros limit:1 status:open --current-patch-set
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
ssh -p 29418 peulerosci@dgggit09-rd.huawei.com gerrit query topic:linux-4.19_src_to_build project:euleros/third_party/open_source/kernelspace/linux-4.19-euleros limit:1 status:open --current-patch-set > /tmp/kernel-src-to-build
if cat /tmp/kernel-src-to-build | grep 'rowCount: 1' &> /dev/null; then
    git_ref=`cat /tmp/kernel-src-to-build | grep 'ref: refs' | awk '{print $2}'`
    old_dts_ar=`cat /tmp/kernel-src-to-build | grep 'commit_detail: ' | awk '{print $2}'`
    ChangeId=`cat /tmp/kernel-src-to-build | grep 'Change-Id: ' | awk '{print $2}'`
else
    git_ref=''
    old_dts_ar=''
    ChangeId=''
fi

git_update "http://peulerosci@dgggit09-rd.huawei.com/a/euleros/third_party/open_source/kernelspace/linux-4.19-euleros" xdf_test
pushd $git_root/linux-4.19
curl -kLo `git rev-parse --git-dir`/hooks/commit-msg http://peulerosci@dgggit09-rd.huawei.com/tools/hooks/commit-msg
chmod +x `git rev-parse --git-dir`/hooks/commit-msg
if [ "x$git_ref" != "x" ]; then
    expect -c "
        set timeout -1
        spawn git fetch http://peulerosci@dgggit09-rd.huawei.com/a/euleros/third_party/open_source/kernelspace/linux-4.19-euleros $git_ref && git checkout FETCH_HEAD
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
fi
popd

git_update "http://dgggit09-rd.huawei.com/a/euleros/third_party/open_source/kernelspace/linux-4.19-euleros" xdf_test
pushd $git_root/linux-4.19-euleros
git diff 84df9525b0c27f3ebc2ebb1864fa62a97fdedb7d > $git_root/linux-4.19/patches/all.patch
commit_log="`git log --no-merges --pretty=format:'%b' -n1 | tac |sed '1,2d'|tac`"
if [ "x$old_dts_ar" != "x" ]; then
    dts_ar=`echo "$commit_log" | grep 'commit_detail: '`
    if ! echo $dts_ar | grep $old_dts_ar &> /dev/null; then
        new_dts_ar="$dts_ar $old_dts_ar"
        commit_log=`echo "$commit_log" | sed "s#commit_detail: .*#$new_dts_ar#g"`
    fi
fi
popd


pushd $git_root/linux-4.19
#last10_id=`git log --oneline -n100 | awk '{print $3}'`
if git log -n100 | grep $GERRIT_PATCHSET_REVISION &> /dev/null; then
    echo "no need to retrigger. already update linux-4.19"
    exit 0
fi

#spec文件必须包含%define hwrelease
#hwrelease h后的数字为release递增字段

for fspec in `ls *.spec`
do
    if grep '%define hwrelease' $fspec &>/dev/null; then
        release=`cat $fspec | grep '%define hwrelease' | awk '{print $3}'`
        if ! echo $release | grep 'h' &>/dev/null; then
            echo "Error: can not find delimit h in $fspec hwrelease"
            exit 1
        fi
        increase_num=`echo $release | awk -F'h' '{print $2}'`
        let new_increase_num=$increase_num+1
        new_release=`echo $release | sed "s#h${increase_num}#h${new_increase_num}#g"`
        sed -i "/define hwrelease/s/$release/$new_release/g" $fspec
    else
        echo "Error: can not find hwrelease in $fspec"
        exit 1
    fi
    version_relase=`rpm -q --qf '%{VERSION}-%{RELEASE}\n' --specfile kernel.spec | head -n1`
    changelog="* `date '+%a %b %d %Y'` ${GERRIT_CHANGE_OWNER_EMAIL%%@*}<${GERRIT_CHANGE_OWNER_EMAIL}> - ${version_relase}\n- Type:bugfix\n- ID:NA\n- SUG:restart\n- DESC:${GERRIT_CHANGE_SUBJECT#*:}\n"
    sed -i "/%changelog/a${changelog}" $fspec
done

owner_name="`echo ${GERRIT_CHANGE_OWNER_NAME:0:1}``echo ${GERRIT_CHANGE_OWNER_NAME} | awk '{print $2}'`"
git config --global user.name ${owner_name}
git config --global user.email ${GERRIT_CHANGE_OWNER_EMAIL}
git add patches/all.patch *.spec
#git log -n3
if [ "x$git_ref" != "x" ]; then
    git_commit_amend "$GERRIT_CHANGE_SUBJECT

$commit_log
from linux-4.19-euleros: $GERRIT_PATCHSET_REVISION

Change-Id: $ChangeId"
else
    git_commit "$GERRIT_CHANGE_SUBJECT

$commit_log
from linux-4.19-euleros: $GERRIT_PATCHSET_REVISION"
fi
git push origin HEAD:refs/for/xdf_test%topic=linux-4.19_src_to_build
git checkout xdf_test
git reset --hard HEAD~~2

url=`ssh -p 29418 peulerosci@dgggit09-rd.huawei.com gerrit query topic:linux-4.19_src_to_build project:euleros/third_party/open_source/kernelspace/linux-4.19 limit:1 status:open --current-patch-set | grep 'url: ' | awk '{print $2}'`

echo "$url" > /tmp/kernel-src-to-build_url
